package com.qf.test;

import com.qf.pojo.User;
import org.junit.Test;
import org.springframework.util.SerializationUtils;
import redis.clients.jedis.Jedis;

import java.security.Key;
import java.util.Date;

/**
 * @author Wy
 * @version V1.0
 * @Project MavenDemo
 * @Package com.qf.test
 * @Description:
 * @Date 2022/6/29 11:10
 */
public class Demo2 {
    @Test
    public void setByteArray() {
        //1. 连接Redis服务
        Jedis jedis = new Jedis("192.168.200.129", 6379);
        //2.1 准备key(String)-value(User)
        String key ="user";
        User user = new User();
        user.setId(1);
        user.setName("wowo");
        user.setBirthday(new Date());
        //2.2 将key和value转换为byte[]
        byte[] serialize = SerializationUtils.serialize(key);
        byte[] serialize1 = SerializationUtils.serialize(user);
        //2.3 将key和value存储到Redis
        jedis.set(serialize, serialize1);
        jedis.close();
    }

    @Test
    public void get() {
        Jedis jedis = new Jedis("192.168.200.129", 6379);
        String key="user";
        byte[] serialize = SerializationUtils.serialize(key);
        byte[] bytes = jedis.get(serialize);
        User deserialize = (User) SerializationUtils.deserialize(bytes);
        System.out.println(deserialize);
        jedis.close();
    }
}
